module Translate
  # Set the limit of translated number of digits
  DIGITS_LIMIT = 5

  def in_english
    return nil if over_limit?
    
    num < 0 ? "minus-#{in_words}" : in_words
  end

  private

  def over_limit?
    digits_count > DIGITS_LIMIT
  end

  def numbers_in_words
    return YAML.load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'languages/english_numbers.yml'))
  end
  
  def in_words
    return single_digits(formatted_num) unless digits_count > 1
    "#{single_digits(digits_count).capitalize}Digits".constantize.send(:run, formatted_num)
  end
  
  def in_array
    formatted_num.to_s.split(//)
  end
  
  def digits_count
    in_array.count
  end
  
  def power_of_ten
    digits_count - 1
  end
  
  def single_digits(number)
    numbers_in_words['ones'][number]
  end
  
  def prefix(number)
    numbers_in_words['powers'][number]
  end
  
  def formatted_num
    num.abs.to_i
  end
  
end